/*
 * This file is part of ELCube.
 *
 * ELCube is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ELCube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
 */
package cn.nkpro.elcube.docengine.controller;

import cn.nkpro.elcube.annotation.NkNote;
import cn.nkpro.elcube.docengine.service.NkPipelineService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by bean on 2020/6/9.
 */
@Slf4j
@NkNote("4.流水线")
@RestController
@RequestMapping("/pipeline")
public class PipelineController {

    @Autowired
    private NkPipelineService pipelineService;

    @PreAuthorize("authenticated")
    @NkNote("1.调用流水线")
    @RequestMapping(value = "/{docType}")
    public Object list(HttpServletRequest request, @PathVariable String docType) {

        if(log.isInfoEnabled()){
            log.info("Pipeline Invoke : " + docType + ", params = "+ JSON.toJSONString(request.getParameterMap()));
        }
        return pipelineService.invoke(
                docType,
                request.getParameterMap()
                        .entrySet()
                        .stream()
                        .collect(Collectors.toMap(Map.Entry::getKey, e->e.getValue()[0]))
        );
    }
}
